package com.creator.jobmatcherresume.service;

import com.creator.jobmatcherresume.entity.JobPosition;
import com.creator.jobmatcherresume.entity.Resume;
import com.creator.jobmatcherresume.tools.CosineSimilarity;
import com.creator.jobmatcherresume.tools.SimilarityCalculator;
import com.creator.jobmatcherresume.tools.TextPreprocessor;

import java.util.HashMap;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: Hank.GUO
 * @Date: 2025/3/25 22:11
 * @Email: guohaihang0512@163.com
 * @Description
 */
// 匹配服务
public class JobMatcherService {
    private SimilarityCalculator similarityCalculator;
    private TextPreprocessor preprocessor;

    public JobMatcherService() {
        this.similarityCalculator = new CosineSimilarity();
        this.preprocessor = new TextPreprocessor();
    }

    public Map<String, Double> matchResumeToJobs(Resume resume, List<JobPosition> positions) {
        Map<String, Double> scores = new HashMap<>();
        Set<String> resumeKeywords = preprocessor.extractKeywords(resume.getContent());

        for (JobPosition position : positions) {
            Set<String> jobKeywords = position.getKeywords();
            double score = similarityCalculator.calculateSimilarity(jobKeywords, resumeKeywords);
            scores.put(position.getId(), score);
        }
        return scores.entrySet().stream()
                .sorted(Map.Entry.<String, Double>comparingByValue().reversed())
                .collect(Collectors.toMap(
                        Map.Entry<String, Double>::getKey,
                        Map.Entry::getValue,
                        (e1, e2) -> e1,
                        LinkedHashMap::new));
    }
}
